<template>
  <div id="menu">
    <el-row>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="菜单名称">
          <el-input v-model="form.menu"></el-input>
        </el-form-item>
        <el-form-item label="url">
          <el-input v-model="form.url"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
          <el-button>取消</el-button>
          <router-link :to="{ name: 'RoleMenu'}">添加权限</router-link>
        </el-form-item>
      </el-form>
    </el-row>

    <el-row>
      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="ID">
        </el-table-column>
        <el-table-column
          prop="menu"
          label="菜单名称">
        </el-table-column>
        <el-table-column
          prop="url"
          label="url">
        </el-table-column>
      </el-table>
    </el-row>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        tableData: [],
        form: {
          menu: '',
          url: '',
          account: sessionStorage.getItem('account')
        }
      }
    },
    methods: {
      init () {
        this.getMenu()
      },
      getMenu () {
        let that = this
        this.$axios({
          method: 'get',
          url: '/api/AuthService.getMenu',
          params: {'account': sessionStorage.getItem('account')}
        })
        .then(function (resp) {
          if (resp.data.status === 'success') {
            that.tableData = resp.data.data
          } else {
            that.$message({
              showClose: true,
              message: resp.data.cause,
              type: 'warning'
            })
          }
        })
      },
      onSubmit () {
        let that = this
        this.$axios({
          method: 'get',
          url: '/api/AuthService.addMenu',
          params: this.form
        })
        .then(function (resp) {
          if (resp.data.status === 'success') {
            that.$message({
              showClose: true,
              message: '添加成功',
              type: 'success'
            })
            that.init()
          } else {
            that.$message({
              showClose: true,
              message: resp.data.cause,
              type: 'warning'
            })
          }
        })
      }
    },
    mounted () {
      this.init()
    }
  }
</script>


<style>
#menu .el-row {
  margin-bottom: 16px;
}
</style>
